/*
 * Copyright 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.compose.material3

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.testutils.assertIsEqualTo
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.getUnclippedBoundsInRoot
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import kotlinx.coroutines.test.StandardTestDispatcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class HorizontalCenterOpticallyTest {
    private val TextTag = "text"
    private val ContainerTag = "container"
    @get:Rule val rule = createComposeRule(StandardTestDispatcher())

    @OptIn(ExperimentalMaterial3ExpressiveApi::class)
    @Test
    fun horizontalCenterOptically_contentPadding_asymmetricShape() {
        val shape =
            RoundedCornerShape(
                topStart = 20.dp,
                bottomStart = 20.dp,
                topEnd = 0.dp,
                bottomEnd = 0.dp,
            )
        val baseContentPadding = PaddingValues(horizontal = 20.dp)
        val expectedStartPadding = 20.dp + (CenterOpticallyCoefficient * 20f).dp
        val expectedEndPadding = 20.dp - (CenterOpticallyCoefficient * 20f).dp
        rule.setContent {
            val layoutDirection = LocalLayoutDirection.current
            Box(modifier = Modifier.clip(shape).testTag(ContainerTag)) {
                Row(
                    modifier =
                        Modifier.horizontalCenterOptically(
                                shape = shape,
                                maxStartOffset =
                                    baseContentPadding.calculateStartPadding(layoutDirection),
                                maxEndOffset =
                                    baseContentPadding.calculateEndPadding(layoutDirection),
                            )
                            .padding(baseContentPadding)
                ) {
                    Text(text = "Test", modifier = Modifier.testTag(TextTag))
                }
            }
        }

        val containerBounds = rule.onNodeWithTag(ContainerTag).getUnclippedBoundsInRoot()
        val textBounds = rule.onNodeWithTag(TextTag).getUnclippedBoundsInRoot()

        (textBounds.left - containerBounds.left).assertIsEqualTo(expectedStartPadding)
        (containerBounds.right - textBounds.right).assertIsEqualTo(expectedEndPadding)
    }

    @OptIn(ExperimentalMaterial3ExpressiveApi::class)
    @Test
    fun horizontalCenterOptically_contentPadding_symmetricShape() {
        val shape = RoundedCornerShape(0.dp)
        val baseContentPadding = PaddingValues(horizontal = 20.dp)
        val expectedPadding = 20.dp
        rule.setContent {
            val layoutDirection = LocalLayoutDirection.current
            Box(modifier = Modifier.clip(shape).testTag(ContainerTag)) {
                Row(
                    modifier =
                        Modifier.horizontalCenterOptically(
                                shape = shape,
                                maxStartOffset =
                                    baseContentPadding.calculateStartPadding(layoutDirection),
                                maxEndOffset =
                                    baseContentPadding.calculateEndPadding(layoutDirection),
                            )
                            .padding(baseContentPadding)
                ) {
                    Text(text = "Test", modifier = Modifier.testTag(TextTag))
                }
            }
        }

        val containerBounds = rule.onNodeWithTag(ContainerTag).getUnclippedBoundsInRoot()
        val textBounds = rule.onNodeWithTag(TextTag).getUnclippedBoundsInRoot()

        (textBounds.left - containerBounds.left).assertIsEqualTo(expectedPadding)
        (containerBounds.right - textBounds.right).assertIsEqualTo(expectedPadding)
    }

    @OptIn(ExperimentalMaterial3ExpressiveApi::class)
    @Test
    fun horizontalCenterOptically_contentPadding_asymmetricShape_rtl() {
        val shape =
            RoundedCornerShape(
                topStart = 20.dp,
                bottomStart = 20.dp,
                topEnd = 0.dp,
                bottomEnd = 0.dp,
            )
        val baseContentPadding = PaddingValues(horizontal = 20.dp)
        val expectedStartPadding = 20.dp + (CenterOpticallyCoefficient * 20f).dp
        val expectedEndPadding = 20.dp - (CenterOpticallyCoefficient * 20f).dp
        rule.setContent {
            CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
                Box(modifier = Modifier.clip(shape).testTag(ContainerTag)) {
                    Row(
                        modifier =
                            Modifier.horizontalCenterOptically(
                                    shape = shape,
                                    maxStartOffset =
                                        baseContentPadding.calculateStartPadding(
                                            LayoutDirection.Rtl
                                        ),
                                    maxEndOffset =
                                        baseContentPadding.calculateEndPadding(LayoutDirection.Rtl),
                                )
                                .padding(baseContentPadding)
                    ) {
                        Text(text = "Test", modifier = Modifier.testTag(TextTag))
                    }
                }
            }
        }

        val containerBounds = rule.onNodeWithTag(ContainerTag).getUnclippedBoundsInRoot()
        val textBounds = rule.onNodeWithTag(TextTag).getUnclippedBoundsInRoot()

        (textBounds.left - containerBounds.left).assertIsEqualTo(expectedEndPadding)
        (containerBounds.right - textBounds.right).assertIsEqualTo(expectedStartPadding)
    }
}
